package com.AAL.DAO;

import java.util.List;

import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IRequestingDetailsForTransferingDAO;
import com.AAL.Model.RequestingDetail;
import com.AAL.Model.RequestingDetailsForTransfering;
import com.AAL.Utils.HibernateUtil;

public class RequestingDetailsForTransferingDAO implements
		IRequestingDetailsForTransferingDAO {

	@Override
	public RequestingDetailsForTransfering saveAndUpdateRequestingDetailsForTransfering(
			RequestingDetailsForTransfering requestingDetailsForTransfering) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(requestingDetailsForTransfering);
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return requestingDetailsForTransfering;
	}

	@Override
	public RequestingDetailsForTransfering deleteRequestingDetailsForTransfering(
			int requesetDetailID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			RequestingDetailsForTransfering requestingDetail = (RequestingDetailsForTransfering) session
					.load(RequestingDetailsForTransfering.class,
							requesetDetailID);
			if (null != requestingDetail) {
				session.delete(requestingDetail);
			}
			session.getTransaction().commit();
			return requestingDetail;
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
	}

	@Override
	public RequestingDetailsForTransfering getRequestingDetailsForTransferingInfor(
			int requestDetailID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		RequestingDetailsForTransfering requestingDetail = (RequestingDetailsForTransfering) session
				.load(RequestingDetailsForTransfering.class, requestDetailID);
		return requestingDetail;
	}
	//Phatdt hien thi chi tiet phieu dieu phoi start
	@SuppressWarnings("unchecked")
	public List<RequestingDetailsForTransfering> GetTransferRequestingDetailByTransferingRequestID(
			String transferingRequestID) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<RequestingDetailsForTransfering> lstrequestingDetail = null;
		
		try{
			lstrequestingDetail = (List<RequestingDetailsForTransfering>) session
					.createQuery(
							"from RequestingDetailsForTransfering where transferingRequest.transferingRequestID=:transferingRequestID")
					.setParameter("transferingRequestID",transferingRequestID).list();
		}catch (Exception e) {
			System.out.println("E:"+e.getMessage());
			return null;
		}		
		return lstrequestingDetail;
	}
	//Phatdt hien thi chi tiet phieu dieu phoi end

}
